define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'point/regionconfig/index',
                    add_url: 'point/regionconfig/add',
                    edit_url: 'point/regionconfig/edit',
                    del_url: 'point/regionconfig/del',
                    multi_url: 'point/regionconfig/multi',
                    table: 'region_config',
                }
            });

            var table = $("#table");
            var region = Config.region;
            var province = Config.province;
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'tag.name', title: __('Tag_id'),placeholder: __('模糊查询'),operate: 'LIKE %...%'},
                        {field : 'region_id', title:'大区',searchList:region,operate:'LIKE %...%',
                            formatter:function(value,row,index){
                                if(row.area['region']){
                                    return row.area['region'];
                                }else{
                                    return '-';
                                }
                            }
                        },                 
                        {field : 'province_id', title:'省',searchList:province,operate:'LIKE %...%',
                            formatter:function(value,row,index){
                                if(value){
                                    return row.area['name'];
                                }else{
                                    return '-';
                                }
                            }
                        },                 
                        {field: 'exportband', title: __('Exportband'), operate:'BETWEEN',placeholder: __('数字')},
                        /*{field: 'transband', title: __('Transband'), operate:'BETWEEN',placeholder: __('数字'),formatter:function(data,full){
                            if(data==0){
                               return '-';
                            }else{
                               return (data).toFixed(2);
                            }
                        }},*/
                        /*{field: 'payment', title: __('Payment'),searchList:paymentList,formatter: Table.api.formatter.normal},*/
                        {field: 'template', title: __('Template'),searchList:templateList,formatter: Table.api.formatter.normal},
                        {field: 'type', title: __('Type'),searchList:regiontypeList,formatter: Table.api.formatter.normal},
                        {field: 'comment',title: __('备注'),placeholder: __('模糊查询'),operate: 'LIKE %...%',formatter:function(value,row,index){
                            var valuerep=value.replace(/\s*/g,"");
                            if(valuerep){
                                    return '<div style="min-width:50px;">' + value + '</div><input type="text" class="form-control text-center" data-field="' + this.field + '" style="width:100%;display:none;min-width:50px;" value="' + value + '">';
                                
                                 }else{
                                    return '<div style="min-width:50px;">' + '<span>&nbsp;</span>'+ '</div><input type="text" class="form-control text-center" data-field="' + this.field + '" style="width:100%;display:none;min-width:50px;" value="">';
                           
                                 }
                            }, events: {

                                'click div': function (e, value, row, index) {
                                    $(this).hide();
                                    $(this).next().show().focus();
                                },
                                'keyup input': function (e, value, row, index) {
                                    if (e.keyCode == 13 || e.keyCode == 10) {
                                        $(this).trigger("blur");
                                    }
                                },
                                'blur input': function (e, value, row, index) {
                                    
                                    var that = this;
                                    var values = $(this).val();
                                    var values1=$(this).prev().text();
                                    if(values1.replace(/\s*/g,"")=="" && values.replace(/\s*/g,"") == ""){
                                        $(that).prev().html('<span>&nbsp;</span>').show();
                                        $(that).attr("value","");
                                        $(that).hide();
                                        $(that).prev().show();
                                        return false;
                                    }
                                    if (values1 != values) {
                                        var data = {};
                                        data["row[" + $(that).data("field") + "]"] = values;
                                        Fast.api.ajax({
                                            url:  "point/regionconfig/editconfcomment?ids=" + row.id,
                                            data: data
                                        }, function (data, ret) {
                                            $(that).hide();
                                            if(values.replace(/\s*/g,"") == ""){
                                                $(that).prev().html('<span>&nbsp;</span>').show();
                                                $(that).attr("value","");
                                                $(this).val("");
                                            }else{
                                                $(that).prev().text(values).show();
                                                $(that).attr("value",values);
                                                $(this).val(values);
                                            }
                                            
                                        });
                                    } else {
                                        $(that).hide();
                                        $(that).prev().show();
                                    }
                                }
                        }},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {

                                    name: 'addtabs',
                                    text: '绑定标签',
                                    title: '设备监控',
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    icon: 'fa fa-plus',
                                    url: '/admin/region/device/index/ids/',
                                    visible: function (row,full) { 
                                        if(row.ids==0){
                                            full.url = "/admin/region/device.html?ref=addtabs";
                                        }else{
                                            full.url ='/admin/region/device/index/ids/'+row.ids+'?ref=addtabs';
                                        }
                                        return true;
                                     }
                                 },
                                 {

                                    name: 'detail',
                                    text: '配置日报',
                                    title: '配置日报',
                                    classname: 'btn btn-xs btn-info btn-addtabs',
                                    icon: 'fa fa-folder-o',
                                    url: '/admin/point/Configcontent/index?name=1',
                                    visible: function (row,full) { 
                                        if(row.tag){
                                            full.url ='/admin/point/Configcontent/index?tag.name='+row.tag.name;
                                        }
                                        return true;
                                     }
                                 }
                            ]}
                    ]
                ]
            });

            

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            $('select[name="row[tag_id]"]').attr("disabled","disabled");
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});